package com.Xht.www.servlet;

//后端响应拉黑客户的请求

import com.Xht.www.dao.MerchantBlacklistDAO;
import com.Xht.www.po.MerchantBlacklistPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;

@WebServlet("/BlacklistCustomerServlet")
public class BlacklistCustomerServlet extends HttpServlet {

    private final MerchantBlacklistDAO merchantBlacklistDAO = new MerchantBlacklistDAO();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取参数
        String merchantName = request.getParameter("merchantName");
        String userIdStr = request.getParameter("userId");
        String reason = request.getParameter("reason");

        // 参数验证
        if (merchantName == null || merchantName.trim().isEmpty() ||
                userIdStr == null || userIdStr.trim().isEmpty() ||
                reason == null || reason.trim().isEmpty()) {
            response.getWriter().write("参数不能为空，请检查后重试");
            return;
        }

        Integer userId;
        try {
            userId = Integer.parseInt(userIdStr);
        } catch (NumberFormatException e) {
            response.getWriter().write("客户 ID 必须为有效的整数，请检查后重试");
            return;
        }

        // 创建 MerchantBlacklistPO 对象
        MerchantBlacklistPO merchantBlacklistPO = new MerchantBlacklistPO();
        merchantBlacklistPO.setMerchantName(merchantName);
        merchantBlacklistPO.setUserId(userId);
        merchantBlacklistPO.setBlacklistTime(new Timestamp(System.currentTimeMillis()));
        merchantBlacklistPO.setReason(reason);

        // 插入黑名单信息
        boolean success = merchantBlacklistDAO.insertMerchantBlacklist(merchantBlacklistPO);
        if (success) {
            response.getWriter().write("客户已成功拉黑");
        }
    }
}